package database.manager;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Properties;

public class DatabaseManager {
	private static DatabaseManager	uniqueInstance	= null;
	private static Connection		con				= null;

	private static final String		drivers			= "com.mysql.jdbc.Driver";
	public static Properties		configFile;

	/**
	 * geeft error als database niet bestaat
	 * 
	 * @param db
	 * @param dbName
	 * @param password
	 * @param inlogName
	 */
	private DatabaseManager() {
		configFile = new Properties();
		try {
			configFile.load( this.getClass().getClassLoader().getResourceAsStream( "\\properties.conf" ) );
			String db = configFile.getProperty( "DB" );
			String dbName = configFile.getProperty( "DBNAME" );
			String inlogName = configFile.getProperty( "USER" );
			String password = configFile.getProperty( "PASS" );
			if( !dbExists( db, dbName, inlogName, password ) ) {
				System.err.println( "Database constructed;" );
			}
		} catch( IOException e ) {
			e.printStackTrace();
			System.out.println( "Properties file not available!" );
		}
	}

	/**
	 * geeft een nieuwe database terug
	 * 
	 * @param db
	 * @param password
	 * @param inlogName
	 * 
	 * @return de database
	 */
	public static synchronized DatabaseManager getInstance() {
		if( uniqueInstance == null ) {
			uniqueInstance = new DatabaseManager();
		}
		return uniqueInstance;
	}

	/**
	 * connect met de database
	 * 
	 * @param db
	 * @param password
	 * @param inlogName
	 * 
	 * @return
	 */
	private Boolean dbExists( String db, String dbName, String inlogName, String password ) {
		try {
			con = makeConnection( db, dbName, inlogName, password );
			return true;
		} catch( Exception e ) {
			e.printStackTrace();
		}
		return false;
	}

	private Connection makeConnection( String db, String dbName, String inlogName, String password ) throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException {
		if( drivers != null )
			Class.forName( drivers );
		try {
			return DriverManager.getConnection( "jdbc:mysql://" + db + "/" + dbName, inlogName, password );
		} catch( SQLException e ) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * verbreekt connectie met de database
	 */
	public void close() {
		try {
			con.close();
			uniqueInstance = null;
			con = null;
		} catch( SQLException e ) {
			e.printStackTrace();
		}
	}

	/**
	 * returnt de huidige verbinding
	 * 
	 * @return de verbinding
	 */
	public Connection getConnection() {
		return con;
	}

	/**
	 * creates a prepared statement
	 * 
	 * @param statement
	 *            sql statement this also catches all the problems with the
	 *            connection.
	 * @return a prepared statement
	 */
	public PreparedStatement prepareStatement( String statement ) {
		try {
			if( con == null ) {
				System.err.println( "prepareStatment: connection == null" );
			} else if( con.isClosed() ) {
				System.err.println( "prepareStatment: connection is closed" );
			} else {
				PreparedStatement ppstm = con.prepareStatement( statement );
				return ppstm;
			}
		} catch( SQLException e ) {
			e.printStackTrace();
		}
		return null;
	}
}
